home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
8_12.lha
/
8_12
/
8_12b5.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
3KB
|
165 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ match a pattern on the input stream
/ %d looks for an integer & stops at the first
/ non-integer character
/ %f looks for a floating point number & stops ...
/ %c matches a single character
/ %s matches a white-space delineated string
/ %% matches %
/ ' ' matches any white space
/ 'X' matches the character X
nt pat::match(char *pattern)
// reset base ptr to pt to beginning of buffer
ebp->setbuffering();
// loop through the pattern looking
// for things to match.
int goodpattern = 1;
while (*pattern && goodpattern)
{
int c = ebp->sgetc();
char p = *pattern++;
/ define a couple of small macros for
/ some common sequences
define Next() \
ebp->stossc(), c = ebp->sgetc()
define doloop(testfunc) \
do { Next(); } while (testfunc(c))
switch (p)
{
// space matches arbitrary white space
case ' ':
if (isspace(c))
doloop(isspace);
else
goodpattern = 0;
break;
// a pattern: %d, %f, %c, %s, %%
case '%':
p = *pattern++;
// check for patterns which
// toss white space first
if ((p == 'd') || (p == 'f') ||
(p == 's'))
if (isspace(c))
doloop(isspace);
// now skip past the particular type
switch (p)
{
// decimal number
case 'd':
if ((c == '-') || (c == '+'))
Next();
if (isdigit(c))
doloop(isdigit);
else
goodpattern = 0;
break;
// floating point
case 'f':
if ((c == '-') || (c == '+'))
Next();
int digitsbefore = 0;
int hasdecimal = 0;
int digitsafter = 0;
int hasexponent = 0;
if (isdigit(c))
{
digitsbefore = 1;
doloop(isdigit);
}
if (c == '.')
{
hasdecimal = 1;
Next();
if (isdigit(c))
{
digitsafter = 1;
doloop(isdigit);
}
}
if ((c == 'e') || (c == 'E'))
{
hasexponent = 1;
Next();
if ((c == '-') || (c == '+'))
Next();
if (isdigit(c))
doloop(isdigit);
else
goodpattern = 0;
}
if (!((digitsbefore &&
(hasdecimal ||
hasexponent)) ||
digitsafter))
goodpattern = 0;
break;
// any string. leading white
// space was tossed above
case 's':
doloop(!isspace);
break;
// any single character
case 'c':
ebp->stossc();
break;
// match single %
case '%':
if (ebp->sgetc() == '%')
ebp->stossc();
else
goodpattern = 0;
break;
// unknown pattern
default:
goodpattern = 0;
break;
}
break;
// non-pattern, match the character
default:
if (c == p)
ebp->stossc();
else
goodpattern = 0;
break;
}
}
ebp->resetbuffering();
return goodpattern;